package com.whjfxx.terminal.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.google.gson.JsonObject;
import com.whjfxx.terminal.exceptions.BizException;

import java.util.Arrays;
import java.util.Map;
import java.util.StringJoiner;

/**
 * Created by b.c on 2022/4/13 9:42
 */
public class SignUtil {

    /**
     * ic 验证签名
     * @param ic
     * @param sign
     * @return
     */
    public static boolean icSign(String ic, String sign) {
        String encode = MD5Util.encode(ic);
        boolean flag = encode.equals(sign);
        if (!flag) {
            throw BizException.build("签名验证失败");
        }
        return flag;
    }

    /**
     * 验证码验证签名
     * @param accountNo
     * @param verificationCode
     * @param sign
     * @return
     */
    public static boolean verificationCodeSign(String accountNo, String verificationCode, String sign) {
        String encode = MD5Util.encode(accountNo, verificationCode);
        boolean flag = encode.equals(sign);
        if (!flag) {
            throw BizException.build("签名验证失败");
        }
        return flag;
    }

    /**
     * 账号密码登录 验证签名
     * @param accountNo
     * @param password
     * @param sign
     * @return
     */
    public static boolean passwordSign(String accountNo, String password, String sign) {
        String encode = MD5Util.encode(accountNo, password);
        sign = sign.toUpperCase();
        System.out.println("secret[" + encode + "]");
        boolean flag = encode.equals(sign);
        if (!flag) {
            throw BizException.build("签名验证失败");
        }
        return flag;
    }

    private final static String KEY_SIGN = "key_sign";

    public static boolean validSign(Object obj, String func_key) {
        Map<String, String> map = JSON.parseObject(JSON.toJSONString(obj), new TypeReference<Map<String, String>>() {});
        // 校验
        String[] keyArr = new String[map.keySet().size()];
        map.keySet().toArray(keyArr);
        Arrays.sort(keyArr);
        // 校验 签名 （签名 = key按照字母顺序拼接（如果为null，则不加入）然后md5
        StringJoiner sj = new StringJoiner("&");
        for (String key : keyArr) {
            String o = map.get(key);
            if (null == o || KEY_SIGN.equals(key)) {
                continue;
            }
            sj.add(key+"="+o);
        }

        sj.add("key="+func_key);

        System.out.println("data: " + sj.toString());
        String sign = MD5Util.encode(sj.toString());
        String keySign = map.get(KEY_SIGN);
        System.out.println("sign:" + sign);
        System.out.println("keySign:" + keySign);
        boolean success = sign.equals(keySign);
        if (!success) {
            throw BizException.build("签名校验失败");
        }
        return success;
    }

    public static String sign(Object obj, String signKey) {
        Map<String, String> map = JSON.parseObject(JSON.toJSONString(obj), new TypeReference<Map<String, String>>() {});
        // 校验
        String[] keyArr = new String[map.keySet().size()];
        map.keySet().toArray(keyArr);
        Arrays.sort(keyArr);
        // 校验 签名 （签名 = key按照字母顺序拼接（如果为null，则不加入）然后md5
        StringJoiner sj = new StringJoiner("&");
        for (String key : keyArr) {
            String o = map.get(key);
            if (null == o || KEY_SIGN.equals(key)) {
                continue;
            }
            sj.add(key + "=" + o);
        }
        sj.add("key="+signKey);
        String sign = MD5Util.encode(sj.toString());
        return sign;
    }

}
